1. /* sdfcosrn.cpp by K.Tsuru */
  2. // function ID 3205 DRADIX
  3. /******************************************************************
  4. SDouble class
  5. trigonometric function cos x using x/R^n reduction method
  6. [Algorithm]
  7. Pay attention to a formura
  8. cos(4*x) = 8*cos(x)^4 - 8*cos(x)^2 + 1.
  9. step 1 : Let R = 4 and d = x/(R^n)(n is an integer).
  10. step 2 : Evaluate y(n) = cos d by series.
  11. step 3 : While n > 0, repeat the recurrence formula
  12. y(n-1) <-- 8*y(n)^4 - 8*y(n)^2 + 1, n <-- n-1.
  13. It is better to adjust the value of n examining the effective figures.
  14. When n is too large,it becomes slower.
  15. Using the conbination with a formula
  16. cos(2*x)=2*cos(x)^2-1
  17. and taking R=8,it becomes faster a little.
  18. *******************************************************************/
  19. #ifndef SN_H
  20. #include "sn.h"
  21. #endif
  22. SDouble CosRN(const SDouble& x){ // |x| <= pi/4
  23. int k;
  24. const uint R = 8u;
  25. const double logR = log10((double)R); // ver. 2.17
  26. uint effFig = x.EffFig(), upPrec;
  27. double pw = log10((double)effFig);
  28. const int n = int(0.5*DFIGURES*pw*pw) + x.RdxExp();
  29. if(n <= 0) return CosSeries(x); // enough small
  30. // n > 0
  31. k = int( (double)n*logR )+ 1; //figures of R^n
  32. upPrec = k/DFIGURES + (uint)pw + 1u;
  33. if(k % DFIGURES) upPrec++;
  34. RealSize C;
  35. SDouble y, u;
  36. uint up = x.ProperUpPrec(upPrec);
  37. //Raises up the precision if possible.
  38. if(up) C.SetEffFig(effFig + up, C.TEMP_EXTEND);
  39. u = x/Dpow(R, n); // X = x/(R^n)
  40. y = CosSeries(u); // CosSeries(u);
  41. k = n;
  42. x.iterationCount = k;
  43. while(k > 0){
  44. //double angle formula
  45. y = DsMult(y*y, 2) -ONE;
  46. //four times angle formula
  47. //faster than a method using double angle formula three times.
  48. u = y*y;
  49. u = u*u-u;
  50. y = DsMult(u, 8) + ONE;
  51. k--;
  52. }
  53. if(up){
  54. C.SetEffFig(0);
  55. y.Reform(3205);
  56. }
  57. return y;
  58. }

sdfcosrn.cpp : last modifiled at 2017/09/07 15:09:42(1,822 bytes)
created at 2017/10/07 10:22:50
The creation time of this html file is 2017/10/07 11:29:39 (Sat Oct 07 11:29:39 2017).